/*******************************************************************************
 * Copyright (c) 2001, 2002 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui;

import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.core.internal.api.PerforceServer;
import net.sourceforge.perforce.ui.internal.Policy;
import net.sourceforge.perforce.ui.internal.ServerDetail;
import net.sourceforge.perforce.ui.internal.ServerDetailComposite;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.team.core.TeamException;
import org.eclipse.ui.dialogs.PropertyPage;

/**
 * @version $Revision: 488 $
 * @author <a href="bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
 */
public class PerforceServerInfoPropertyPage extends PropertyPage {
  // ---------------------------------------------------------------------------
  // Private Variables

  /** Stores the Perforce Server. */
  private IPerforceServer perforceServer;
  /** Stores the composite for displaying the server's details. */
  private ServerDetailComposite serverDetailComposite;

  // ---------------------------------------------------------------------------
  // Protected Methods

  @Override
  protected Control createContents(Composite parent) {
    initialize();
    noDefaultAndApplyButton();

    Composite outerComposite = new Composite(parent, SWT.NONE);
    outerComposite.setLayout(new GridLayout());
    outerComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    Composite composite = new Composite(outerComposite, SWT.NULL);
    GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    composite.setLayout(layout);
    composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    try {
      createLabel(composite, Policy.bind("PerforceServerInfoPropertyPage.serverAddress")); //$NON-NLS-1$
      createLabel(composite, perforceServer.getServerAddress());

      createLabel(composite, Policy.bind("PerforceServerInfoPropertyPage.serverRoot")); //$NON-NLS-1$
      createLabel(composite, perforceServer.getServerRoot());

      createLabel(composite, Policy.bind("PerforceServerInfoPropertyPage.serverDate")); //$NON-NLS-1$
      createLabel(composite, perforceServer.getServerDate());

      createLabel(composite, Policy.bind("PerforceServerInfoPropertyPage.serverVersion")); //$NON-NLS-1$
      createLabel(composite, perforceServer.getServerVersion());

      createLabel(composite, Policy.bind("PerforceServerInfoPropertyPage.serverLicense")); //$NON-NLS-1$
      createLabel(composite, perforceServer.getServerLicense());

      Label separator = new Label(outerComposite, SWT.HORIZONTAL | SWT.SEPARATOR);
      separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

      serverDetailComposite =
          new ServerDetailComposite(outerComposite, ServerDetailComposite.AUTHENTICATION);
      serverDetailComposite.setServerDetail(new ServerDetail(perforceServer));
      serverDetailComposite.setFocus();
    } catch (TeamException e) {
      PerforceUIPlugin.log(e.getStatus());
    }

    applyDialogFont(composite);
    return outerComposite;
  }

  @Override
  public boolean performOk() {
    try {
      final ServerDetail detail = serverDetailComposite.getServerDetail();

      perforceServer.setClient(detail.getClient());
      perforceServer.setHost(detail.getHost());

      perforceServer.setUser(detail.getUser());
      perforceServer.setPassword(detail.getPassword());
      ((PerforceServer) perforceServer).updateCache();

      perforceServer.setShouldIncludeInBackgroundUpdates(detail.getBackgroundUpdates());
      perforceServer.setCharset(detail.getCharacterSet());
      ((PerforceServer) perforceServer).invalidateConnections();

      PerforceCorePlugin.getServerManager().performSnapshot();

      return super.performOk();
    } catch (PerforceException e) {
      PerforceUIPlugin.log(e.getStatus());
      return false;
    }
  }

  //----------------------------------------------------------------------------
  // - Private Methods

  /** Called to initialize the property page. */
  private void initialize() {
    IAdaptable element = getElement();
    if (element instanceof IPerforceServer) {
      perforceServer = (IPerforceServer) element;
    } else {
      Object adapter = element.getAdapter(IPerforceServer.class);
      if (adapter instanceof IPerforceServer) {
        perforceServer = (IPerforceServer) adapter;
      }
    }
  }

  /**
   * Called to create a label, setting the horizontal span to the given value.
   * 
   * @param parent the parent composite.
   * @param text the label's text.
   * @param span the horizontal span.
   * 
   * @return the new Label.
   */
  private Label createLabel(Composite parent, String text, int span) {
    Label label = new Label(parent, SWT.LEFT);
    label.setText(text);
    GridData data = new GridData();
    data.horizontalSpan = span;
    data.horizontalAlignment = GridData.FILL;
    label.setLayoutData(data);
    return label;
  }

  /**
   * Called to create a label, setting the horizontal span to the given one.
   * 
   * @param parent the parent composite.
   * @param text the label's text.
   * 
   * @return the new Label.
   */
  private Label createLabel(Composite parent, String text) {
    return createLabel(parent, text, 1);
  }

  /**
   * Utility method to create a Text field.
   * 
   * @param parent the parent composite.
   * @return the new Text field.
   */
  static Text createTextField(Composite parent) {
    // ==( PRE-CONDITION
    // )======================================================================
    Policy.assertNotNull(parent);
    // ==( PRE-CONDITION
    // )======================================================================

    Text result = new Text(parent, SWT.SINGLE | SWT.BORDER);

    GridData data = new GridData(GridData.FILL_HORIZONTAL);
    data.verticalAlignment = GridData.CENTER;
    data.grabExcessVerticalSpace = false;
    data.grabExcessHorizontalSpace = true;
    data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
    result.setLayoutData(data);

    return result;
  }
}
